package cl.easyRestaurant.dataAccess.persistence;

// Generated 13-jul-2012 21:11:20 by Hibernate Tools 3.3.0.GA

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Home object for domain model class Pedido.
 * @see cl.easyRestaurant.dataAccess.persistence.Pedido
 * @author Hibernate Tools
 */
@Stateless
public class PedidoHome {

	private static final Log log = LogFactory.getLog(PedidoHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public void persist(Pedido transientInstance) {
		
		log.debug("persisting Pedido instance");
		
		try {
			
			this.entityManager = javax.persistence.Persistence.createEntityManagerFactory("ToherPersistenceUnit").createEntityManager();
			
			entityManager.persist(transientInstance);
			
			log.debug("persist successful");
			
		} catch (RuntimeException re) {
			
			log.error("persist failed", re);
			
			throw re;
			
		}
		finally {
			
			if (this.entityManager != null) {
				
				this.entityManager.close();
				
			}
			
		}

	}

	public void remove(Pedido persistentInstance) {
		log.debug("removing Pedido instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Pedido merge(Pedido detachedInstance) {
		
		log.debug("merging Pedido instance");
		
		try {
			
			this.entityManager = javax.persistence.Persistence.createEntityManagerFactory("ToherPersistenceUnit").createEntityManager();
			
			Pedido result = entityManager.merge(detachedInstance);
			
			log.debug("merge successful");
			
			return result;
			
		} catch (RuntimeException re) {
			
			log.error("merge failed", re);
			
			throw re;
			
		}
		finally {
			
			if (this.entityManager != null) {
				
				this.entityManager.close();
				
			}
			
		}

	}

	public Pedido findById(int id) {
		
		log.debug("getting Pedido instance with id: " + id);
		
		try {
			
			this.entityManager = javax.persistence.Persistence.createEntityManagerFactory("ToherPersistenceUnit").createEntityManager();
			
			Pedido instance = entityManager.find(Pedido.class, id);
			
			log.debug("get successful");
			
			return instance;
			
		} catch (RuntimeException re) {
			
			log.error("get failed", re);
			
			throw re;
			
		}
		finally {
			
			if (this.entityManager != null) {
				
				this.entityManager.close();
				
			}
			
		}
		
	}
	
	public List<Pedido> getAllByClient(String idCliente){
		
		try {
			
			this.entityManager = javax.persistence.Persistence.createEntityManagerFactory("ToherPersistenceUnit").createEntityManager();
			
			TypedQuery<Pedido> q = this.entityManager.createQuery("SELECT p FROM Pedido p WHERE p.cliente.idCli = :idCli", Pedido.class);
			
			q.setParameter("idCli", idCliente);
			
			return q.getResultList();
			
		} catch (RuntimeException re) {
			
			throw re;
			
		}
		finally {
			
			if (this.entityManager != null) {
				
				this.entityManager.close();
				
			}
			
		}
		
	}
	
	public List<Pedido> getAllByChef(String idCocinero){
		
		try {
			
			this.entityManager = javax.persistence.Persistence.createEntityManagerFactory("ToherPersistenceUnit").createEntityManager();
			
			TypedQuery<Pedido> q = this.entityManager.createQuery("SELECT p FROM Pedido p WHERE p.cocinero.idCocinero = :idCocinero", Pedido.class);
			
			q.setParameter("idCocinero", idCocinero);
			
			return q.getResultList();
			
		} catch (RuntimeException re) {
			
			throw re;
			
		}
		finally {
			
			if (this.entityManager != null) {
				
				this.entityManager.close();
				
			}
			
		}
		
	}
	
	public Integer getLastId(){
		
		try {
			
			this.entityManager = javax.persistence.Persistence.createEntityManagerFactory("ToherPersistenceUnit").createEntityManager();
			
			TypedQuery<Integer> q = this.entityManager.createQuery("SELECT MAX(p.idPedido) FROM Pedido p", Integer.class);
			
			return q.getSingleResult();
			
		} catch (RuntimeException re) {
			
			throw re;
			
		}
		finally {
			
			if (this.entityManager != null) {
				
				this.entityManager.close();
				
			}
			
		}
		
	}

}
